package org.example.demo1.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.example.demo1.common.ResponseData;
import org.example.demo1.domain.GoodsInfo;
import org.example.demo1.domain.UserInfo;
import org.example.demo1.mapper.GoodsMapper;
import org.example.demo1.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public ResponseData<GoodsInfo> addGoods(GoodsInfo goodsInfo) {
        if(StrUtil.isBlank(goodsInfo.getGoodsName())){
            return ResponseData.fail("商品名称不能为空");
        }
        if(goodsInfo.getGoodsId() == null){
            return ResponseData.fail("商品编码不能为空");
        }
        if(StrUtil.isBlank(goodsInfo.getGoodsClassify())){
            return ResponseData.fail("商品分类不能为空");
        }
        if(StrUtil.isBlank(goodsInfo.getProduceFactory())){
            return ResponseData.fail("生产厂家不能为空");
        }
        if(StrUtil.isBlank(goodsInfo.getSale())){
            return ResponseData.fail("用户名不能为空");
        }
        GoodsInfo getGoods = goodsMapper.getGoodsByName(goodsInfo.getGoodsName());
        if(ObjectUtil.isNotEmpty(getGoods)){
            return ResponseData.fail("用户已存在",goodsInfo);
        }
        goodsInfo.setCreateTime(new Date());
        int i = goodsMapper.insertGoods(goodsInfo);
        if (i <= 0){
            return ResponseData.fail("请求失败");
        }
        return ResponseData.success(goodsInfo);
    }

    @Override
    public ResponseData<GoodsInfo> updateGoods(GoodsInfo goodsInfo) {
        GoodsInfo getGoods = goodsMapper.getGoodsByName(goodsInfo.getGoodsName());
        goodsInfo.setUpdateTime(new Date());
        if(ObjectUtil.isNotEmpty(getGoods)){
            goodsMapper.updateGoods(goodsInfo);
        }else {
            return ResponseData.fail("用户不存在");
        }
        return ResponseData.success("更新成功",goodsInfo);
    }

    @Override
    public ResponseData<GoodsInfo> deleteGoodsByGoodsId(Integer goodsId) {
        int i=goodsMapper.deleteGoodsByGoodsId(goodsId);
        if(i<1){
            return ResponseData.fail("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData<List<GoodsInfo>> searchGoodsByName(String goodsName) {
        return goodsMapper.selectByGoodsNameLike("%" + goodsName + "%");
    }

    @Override
    public ResponseData<List<GoodsInfo>> searchGoodsById(Integer goodsId) {
        return goodsMapper.selectByGoodsId(goodsId);
    }

    @Override
    public ResponseData<List<GoodsInfo>> pageQuery(int pageNum, int pageSize, String classifyId) {
        return goodsMapper.GoodsInfoPage(pageNum, pageSize, classifyId);
    }
}
